package com.scm.controller;

import com.scm.pojo.Vender;
import com.scm.service.PoItemService;
import com.scm.service.PoMainService;
import com.scm.service.VenderService;
import com.scm.util.Page;
import com.scm.util.QueryResult;
import com.scm.util.Result;
import com.scm.util.Util;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ProviderController
 * @Description TODO
 * @Author Xu
 * @Date 2021/7/9 16:47
 * @Version 1.0
 **/
@RestController
public class ProviderController {
    @Autowired
    private VenderService venderService;

    @Autowired
    private PoMainService poMainService;

    @RequestMapping("/venderList")
    public QueryResult venderList(Integer currentPage, Integer pageSize){
        Map map = Page.get(currentPage,pageSize);
        List list = venderService.getPageVender(map);
        int total = venderService.getAllVender().size();
        return QueryResult.ok(total,list);
    }
    @RequestMapping("/addVender")
    public Result addVender(){
        List<String> list = venderService.getAllVender().stream().map(Vender::getVenderCode).collect(Collectors.toList());
        String venderCode = Util.getID(list, "v");
        return Result.ok(venderCode);
    }
    @RequestMapping("/addVender2")
    public Result addVender2(Vender vender){
        vender.setCreateDate(new SimpleDateFormat("yyyy-MM--dd hh:mm:ss").format (new Date()));
        venderService.addVender(vender);
        return Result.ok(null);
    }
    @RequestMapping("selectVender")
    public QueryResult selectVender(Integer currentPage, Integer pageSize,String venderCode,String name,String contactor){

        Map<String, Object> map = Page.get(currentPage, pageSize);
        map.put("venderCode",venderCode);
        map.put("name",name);
        map.put("contactor",contactor);
        List<Vender> venders = venderService.selectVender(map);
        int total = venderService.selectCountVender(map);
        return QueryResult.ok(total,venders);
    }
    @RequestMapping("/deleteVender")
    public Result deleteVender(String venderCode){
        //待判断----------------------------------------------------
        boolean flag = poMainService.getVender().stream().anyMatch(s -> s.equals(venderCode));
        if (flag){
            return Result.error("存在数据无法删除!!");
        }else {
            venderService.deleteVender(venderCode);
            return Result.ok(null);
        }

    }
    @RequestMapping("/updateVender")
    public Result updateVender(String venderCode){
        Vender vender = venderService.findVender(venderCode);
        return Result.ok(vender);
    }
    @RequestMapping("/updateVender2")
    public Result updateVender2(Vender vender){
        venderService.updateVender(vender);
        return Result.ok(null);
    }
}
